//
//  AntCurrencyDetailViewController.m
//  PeopleStreet
//
//  Created by 辰领科技 on 16/9/21.
//  Copyright © 2016年 yuf. All rights reserved.
//

#import "AntCurrencyDetailViewController.h"
#import "AntCurrencyDetailTableViewCell.h"

@interface AntCurrencyDetailViewController ()
<UITableViewDataSource,UITableViewDelegate>
@property (strong, nonatomic) IBOutlet UIView *navView;
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) IBOutlet UILabel *GoldLabel;
@property (strong, nonatomic) IBOutlet UILabel *silverLabel;
@property (strong, nonatomic) NSMutableArray *consumption;//消费
@property (strong, nonatomic) NSMutableArray *activity;//活动
@property (strong, nonatomic) NSMutableArray *garage;//车库
@property (strong, nonatomic) NSMutableArray *freeze;//冻结

@end

@implementation AntCurrencyDetailViewController

- (void)viewWillAppear:(BOOL)animated{
    [self.tableView.header beginRefreshing];
}


- (void)viewDidLoad {
    [super viewDidLoad];

    _consumption = [NSMutableArray array];
    _activity = [NSMutableArray array];
    _garage = [NSMutableArray array];
    _freeze = [NSMutableArray array];
    [self initViewLayout];
    [self addRefreshing];
}

- (void)initViewLayout{
    [self adaptiveViewLayout:self.view];
    [self adaptiveViewLayout:self.navView];
    
    _tableView.dataSource = self;
    _tableView.delegate = self;
    
    _GoldLabel.text = self.gold;
    _silverLabel.text = self.silver;
}

#pragma mark -- 添加刷新数据
- (void)addRefreshing{
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self getAntCurrencyDetail];
    }];
    [_tableView.header beginRefreshing];
}

- (IBAction)back:(id)sender {
    [self.navigationController  popViewControllerAnimated:YES];
}

#pragma mark == 蚂蚁币明细
- (void)getAntCurrencyDetail{
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    self.shareApi.delegate=self;
    [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"msceMemberId"];
    [dic setObject:[Common getLoginInfo][Key_User_password] forKey:@"musePwd"];
    [self.shareApi  requestOperationWithDic:dic withPath:SX_queryMemberEpurseDetail withHttpType:@"POST" withTag:SX_SX_queryMemberEpurseDetail];
}

#pragma mark -- UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        //消费
        return _consumption.count;
    } else if (section == 1) {
        //活动
        return _activity.count;
    } else if (section == 2) {
        //车库
        return _garage.count;
    } else {
        //冻结
        return _freeze.count;
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50*VerticalRatio();
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    AntCurrencyDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AntCurrencyDetail"];
    if (cell == nil) {
        cell= [[AntCurrencyDetailTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"AntCurrencyDetail"];
        
    }
    for (UIView *view in cell.subviews) {
        if ([view isKindOfClass:[UILabel class]] ||[view isKindOfClass:[UIImageView class]]||[view isKindOfClass:[UIScrollView class]]||[view isKindOfClass:[UIButton class]]) {
            [view removeFromSuperview];
        }
    }
    NSDictionary *dic;
    if (indexPath.section == 0) {
        dic = _consumption[indexPath.row];
        if ([dic.allKeys containsObject:@"mepuEpurse"]) {
            [self setSection1:NO :cell.imageButton :cell.titleLabel :cell.detailLabel :1];
            cell.moneyLabel.text = [NSString stringWithFormat:@"%@",dic[@"mepuEpurse"]];
            cell.moneyLabel.textColor = [UIColor colorWithRed:1.000 green:0.667 blue:0.086 alpha:1.000];
        } else {
            [self setSection1:YES :cell.imageButton :cell.titleLabel :cell.detailLabel :1];
            cell.moneyLabel.text = [NSString stringWithFormat:@"%@",dic[@"mepuSilver"]];
            cell.moneyLabel.textColor = [UIColor colorWithWhite:0.149 alpha:1.000];
        }
        
    } else if (indexPath.section == 1) {
        dic = _activity[indexPath.row];
        if ([dic.allKeys containsObject:@"meacGolden"]) {
            cell.imageButton.selected = NO;
            cell.titleLabel.text = [NSString stringWithFormat:@"活动金币-%@",dic[@"meacActName"]];
            cell.moneyLabel.text = [NSString stringWithFormat:@"%@",dic[@"meacGolden"]];
            cell.moneyLabel.textColor = [UIColor colorWithRed:1.000 green:0.667 blue:0.086 alpha:1.000];
        } else {
            cell.imageButton.selected = YES;
            cell.titleLabel.text = [NSString stringWithFormat:@"活动银币-%@",dic[@"meacActName"]];
            cell.moneyLabel.text = [NSString stringWithFormat:@"%@",dic[@"meacSilver"]];
            cell.moneyLabel.textColor = [UIColor colorWithWhite:0.149 alpha:1.000];
        }
        
        NSString *time = [NSString stringWithFormat:@"%@",dic[@"meacDueTime"]];
        long long beginTime=[time longLongValue];
        NSDate *Date = [[NSDate alloc]initWithTimeIntervalSince1970:beginTime/1000.0];
        NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
        [formatter setDateFormat:@"yyyy.MM.dd"];
        time = [formatter stringFromDate:Date];
        
        cell.detailLabel.text =  [NSString stringWithFormat:@"到期时间%@",time];
        cell.detailLabel.hidden = NO;
        
    } else if (indexPath.section == 2) {
        dic = _consumption[indexPath.row];
        if ([dic.allKeys containsObject:@"mepuParkGolden"]) {
            
            cell.moneyLabel.text = [NSString stringWithFormat:@"%@",dic[@"mepuParkGolden"]];
            cell.moneyLabel.textColor = [UIColor colorWithRed:1.000 green:0.667 blue:0.086 alpha:1.000];
        } else {
            [self setSection1:YES :cell.imageButton :cell.titleLabel :cell.detailLabel :2];
            cell.moneyLabel.text = [NSString stringWithFormat:@"%@",dic[@"mepuParkSilver"]];
            cell.moneyLabel.textColor = [UIColor colorWithWhite:0.149 alpha:1.000];
        }
    } else if (indexPath.section == 3) {
        dic = _activity[indexPath.row];
        if ([dic.allKeys containsObject:@"mefrGolden"]) {
            cell.imageButton.selected = NO;
            cell.titleLabel.text = [NSString stringWithFormat:@"活动金币-%@",dic[@"mefrActName"]];
            cell.moneyLabel.text = [NSString stringWithFormat:@"%@",dic[@"mefrGolden"]];
            cell.moneyLabel.textColor = [UIColor colorWithRed:1.000 green:0.667 blue:0.086 alpha:1.000];
        } else {
            cell.imageButton.selected = YES;
            cell.titleLabel.text = [NSString stringWithFormat:@"活动银币-%@",dic[@"mefrActName"]];
            cell.moneyLabel.text = [NSString stringWithFormat:@"%@",dic[@"mefrSilver"]];
            cell.moneyLabel.textColor = [UIColor colorWithWhite:0.149 alpha:1.000];
        }
        
        NSString *time = [NSString stringWithFormat:@"%@",dic[@"mefrDueTime"]];
        long long beginTime=[time longLongValue];
        NSDate *Date = [[NSDate alloc]initWithTimeIntervalSince1970:beginTime/1000.0];
        NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
        [formatter setDateFormat:@"yyyy.MM.dd"];
        time = [formatter stringFromDate:Date];
        
        cell.detailLabel.text =  [NSString stringWithFormat:@"解冻时间%@",time];
        cell.detailLabel.hidden = NO;
    }
    return cell;
}

- (void)setSection1:(BOOL)isOrNot :(UIButton *)button :(UILabel *)title :(UILabel *)detail :(NSInteger)i{
    if (isOrNot) {
        button.selected = YES;
        if (i == 1) {
            title.text = @"消费金币";
        } else if ( i == 2) {
            title.text = @"车库金币";
        }
        detail.hidden = YES;
    } else {
        button.selected = NO;
        if (i == 1) {
            title.text = @"消费银币";
        } else if ( i == 2) {
            title.text = @"车库银币";
        }
        detail.hidden = YES;
    }
}



#pragma   mark---- GetADBanner Request Delegate

-(void)fetchDatabaseFinished:(NSMutableDictionary *)database withTag:(NSInteger)tag{
    [_tableView.footer endRefreshing];
    [_tableView.header endRefreshing];
    if (tag == SX_SX_queryMemberEpurseDetail){
        if (  [[database valueForKey:@"type"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            NSMutableDictionary *dic = [NSMutableDictionary dictionary];
            dic = database[@"result"];
            if ([dic.allKeys containsObject:@"frozen"]) {
                //冻结
                if (![dic[@"frozen"]isEqual:[NSNull null]]) {
                    NSMutableArray *array = dic[@"frozen"];
                    for (int i = 0 ;i < array.count ; i++) {
                        NSMutableDictionary *dic = array[i];
                        NSMutableDictionary * dict = [NSMutableDictionary dictionaryWithDictionary:dic];
                        if ([dict.allKeys containsObject:@"mefrGolden"]) {
                            if ([dict.allKeys containsObject:@"mefrSilver"]) {
                                [dict removeObjectForKey:@"mefrSilver"];
                            }
                            [_activity addObject:dict];
                        }
                        
                        NSMutableDictionary * Dict = [NSMutableDictionary dictionaryWithDictionary:dic];
                        if ([Dict.allKeys containsObject:@"mefrSilver"]) {
                            if ([Dict.allKeys containsObject:@"mefrGolden"]) {
                                [Dict removeObjectForKey:@"mefrGolden"];
                            }
                            [_activity addObject:Dict];
                        }
                    }
                }
            }
            if ([dic.allKeys containsObject:@"act"]) {
                //活动
                if (![dic[@"act"]isEqual:[NSNull null]]) {
                    NSMutableArray *array = dic[@"act"];
                    for (int i = 0 ;i < array.count ; i++) {
                        NSMutableDictionary *dic = array[i];
                        NSMutableDictionary * dict = [NSMutableDictionary dictionaryWithDictionary:dic];
                        if ([dict.allKeys containsObject:@"meacGolden"]) {
                            if ([dict.allKeys containsObject:@"meacSilver"]) {
                                [dict removeObjectForKey:@"meacSilver"];
                            }
                            [_activity addObject:dict];
                        }
                        
                        NSMutableDictionary * Dict = [NSMutableDictionary dictionaryWithDictionary:dic];
                        if ([Dict.allKeys containsObject:@"meacSilver"]) {
                            if ([Dict.allKeys containsObject:@"meacGolden"]) {
                                [Dict removeObjectForKey:@"meacGolden"];
                            }
                            [_activity addObject:Dict];
                        }
                    }
                }
                
            }
            if ([dic.allKeys containsObject:@"epurse"]) {
                //消费
                if (![dic[@"epurse"]isEqual:[NSNull null]]) {
                    NSDictionary *dic = [NSDictionary dictionary];
                    if ([dic.allKeys containsObject:@"mepuEpurse"]) {
                        [dic setValue:dic[@"mepuEpurse"] forKey:@"mepuEpurse"];
                        [_consumption addObject:dic];
                    }
                    if ([dic.allKeys containsObject:@"mepuSilver"]) {
                        [dic setValue:dic[@"mepuSilver"] forKey:@"mepuSilver"];
                        [_consumption addObject:dic];
                    }
                    //车库
                    if ([dic.allKeys containsObject:@"mepuParkGolden"]) {
                        [dic setValue:dic[@"mepuParkGolden"] forKey:@"mepuParkGolden"];
                        [_garage addObject:dic];
                    }
                    if ([dic.allKeys containsObject:@"mepuParkSilver"]) {
                        [dic setValue:dic[@"mepuParkSilver"] forKey:@"mepuParkSilver"];
                        [_garage addObject:dic];
                    }
                }
            }
            [_tableView reloadData];
        }
    }
    
}


-(void)fetchDatabaseFailed:(NSError *)error message:(NSString *)message{
    [_tableView.footer endRefreshing];
    [_tableView.header endRefreshing];
    [self dismissWaitDialog];
    
    
}

@end
